package xj.graph2d.template;

import java.awt.Color;
import java.util.Map;

import xj.graph2d.ArrowAttr;
import xj.graph2d.DrawAttr;
import xj.graph2d.DrawShapeBase;
import xj.graph2d.EllipseShape;
import xj.graph2d.LineShape;
import xj.graph2d.OpenShape;
import xj.graph2d.RectangleShape;
import xj.graph2d.TextShape;
import xj.graph2d.shapes.CircleShape;
import xj.graph2d.shapes.CubicCurveShape;
import xj.graph2d.shapes.OrthogonalLineShape;
import xj.graph2d.shapes.PolyCurveShape;
import xj.graph2d.shapes.PolyLineShape;
import xj.graph2d.shapes.PolygonShape;
import xj.graph2d.shapes.QuadCurveShape;
import xj.graph2d.shapes.RoundRectangleShape;

public class TestTemplate extends Template {

  static String[] names = {
    // open shapes
    "Line", "QuadCurve", "CubicCurve", "PolyLine", "PolyCurve", "Orthline",
    
    // closed shapes
    "Rectangle", "Ellipse", "RoundRectangle", "Circle", "Polygon", "Text",
  };

  public TestTemplate() {
  }

  @Override
  public void makeTemplate(Map iconMap) {
    for (int i = 0; i < names.length; i++) {
      addButton(names[i], makeShape(names[i]), 0);
      addButton(names[i], makeShape(names[i]), 1);
      addButton(names[i], makeShape(names[i]), 2);
      addButton(names[i], makeShape(names[i]), 3);
    }
  }

  protected DrawShapeBase makeShape(String name) {
    DrawShapeBase shape = null;
    if ("Line".equals(name)) {
      shape = new LineShape(0, 0, 100, 100);
    } else if ("QuadCurve".equals(name)) {
      shape = new QuadCurveShape(0, 0, 100, 100, 100, 0);
    } else if ("CubicCurve".equals(name)) {
      shape = new CubicCurveShape(0, 0, 100, 100, 20, 00, 80, 100);
    } else if ("Rectangle".equals(name)) {
      shape = new RectangleShape(0, 0, 100, 80);
    } else if ("Ellipse".equals(name)) {
      shape = new EllipseShape(0, 0, 100, 80);
    } else if ("RoundRectangle".equals(name)) {
      shape = new RoundRectangleShape(0, 0, 100, 80);
    } else if ("Circle".equals(name)) {
      shape = new CircleShape(50, 50, 50);
    } else if ("Text".equals(name)) {
      TextShape text = new TextShape();
      text.setPrompt("Click to type");
      shape = text;
    } else if ("Polygon".equals(name)) {
      PolygonShape polygon = new PolygonShape(50, 0);
      polygon.addPoint(100, 30);
      polygon.addPoint(80, 100);
      polygon.addPoint(20, 100);
      polygon.addPoint(0, 30);
      polygon.calculateBounds();
      shape = polygon;
    } else if ("PolyLine".equals(name)) {
      PolyLineShape polyline = new PolyLineShape(0, 0);
      polyline.addPoint(50, 25);
      polyline.addPoint(20, 50);
      polyline.addPoint(40, 100);
      polyline.addPoint(100, 70);
      polyline.calculateBounds();
      shape = polyline;
    } else if ("PolyCurve".equals(name)) {
      PolyCurveShape polycurve = new PolyCurveShape(0, 0);
      polycurve.addPoint(90, 30);
      polycurve.addPoint(10, 60);
      // polycurve.addPoint(40, 100);
      polycurve.addPoint(100, 90);
      polycurve.adjustControlPoints();
      polycurve.calculateBounds();
      shape = polycurve;
    } else if ("Orthline".equals(name)) {
      OrthogonalLineShape orthline = new OrthogonalLineShape(20, 0);
      orthline.addPoint(50, 25);
      orthline.addPoint(100, 30);
      orthline.addPoint(60, 60);
      orthline.addPoint(0, 70);
      orthline.addPoint(100, 100);
      orthline.calculateBounds();
      shape = orthline;
    }
    return shape;
  }

  protected void addButton(String name, DrawShapeBase shape, int a) {
    if (name != null && shape != null) {
      String sname = name;
      DrawAttr attr = new DrawAttr();
      shape.setDrawAttr(attr);
      switch (a) {
      case 1:
	attr.setPenColor(Color.BLUE);
	sname += "-blue";
	break;

      case 2:
	attr.setPenColor(Color.GREEN);
	attr.setPenSize(3);
	attr.setLineStyle(DrawAttr.DASHED_LINE);
	sname += "-green";
	break;

      case 3:
	attr.setPenColor(Color.RED);
	attr.setPenSize(2);
	if (shape instanceof OpenShape) {
	  OpenShape oshape = (OpenShape) shape;
	  ArrowAttr headArrowAttr = new ArrowAttr();
	  ArrowAttr tailArrowAttr = new ArrowAttr();
	  headArrowAttr.setType(ArrowAttr.ArrowType.HollowTriangle);
	  headArrowAttr.setSize(16);
	  tailArrowAttr.setType(ArrowAttr.ArrowType.FilledDiamond);
	  tailArrowAttr.setSize(12);
	  oshape.addArrows(headArrowAttr, tailArrowAttr);
	} else {
	  attr.setFillColor(Color.ORANGE);
	  shape.setFilled(true);
	}
	sname += "-red";
	break;
      default:
      }
      add(new TemplateToolButton(sname, shape, 24, 24, false, true));
    }
  }

}
